package cn.edu.sgu.www.mhxysy.service.children.impl;

import cn.edu.sgu.www.mhxysy.pager.Pager;
import cn.edu.sgu.www.mhxysy.consts.CustomDirectories;
import cn.edu.sgu.www.mhxysy.dto.children.ChildrenSkillCategoryDTO;
import cn.edu.sgu.www.mhxysy.entity.children.ChildrenSkillCategory;
import cn.edu.sgu.www.mhxysy.exception.GlobalException;
import cn.edu.sgu.www.mhxysy.mapper.children.ChildrenSkillCategoryMapper;
import cn.edu.sgu.www.mhxysy.pager.children.ChildrenSkillCategoryPager;
import cn.edu.sgu.www.mhxysy.restful.ResponseCode;
import cn.edu.sgu.www.mhxysy.service.children.ChildrenSkillCategoryService;
import cn.edu.sgu.www.mhxysy.util.ImageUpdater;
import cn.edu.sgu.www.mhxysy.util.StringUtils;
import cn.edu.sgu.www.mhxysy.util.UploadUtils;
import cn.edu.sgu.www.mhxysy.util.WrapperUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.function.Consumer;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class ChildrenSkillCategoryServiceImpl implements ChildrenSkillCategoryService {

    private final UploadUtils uploadUtils;
    private final ImageUpdater imageUpdater;
    private final ChildrenSkillCategoryMapper childrenSkillCategoryMapper;

    @Autowired
    public ChildrenSkillCategoryServiceImpl(
            UploadUtils uploadUtils,
            ImageUpdater imageUpdater,
            ChildrenSkillCategoryMapper childrenSkillCategoryMapper) {
        this.uploadUtils = uploadUtils;
        this.imageUpdater = imageUpdater;
        this.childrenSkillCategoryMapper = childrenSkillCategoryMapper;
    }

    @Override
    public String upload(MultipartFile file) throws IOException {
        String directory = CustomDirectories.DIRECTORY_ZNJN;

        return uploadUtils.upload(file, directory);
    }

    @Override
    public void insert(ChildrenSkillCategoryDTO childrenSkillCategoryDTO) {
        ChildrenSkillCategory childrenSkillCategory = toEntity(childrenSkillCategoryDTO);

        childrenSkillCategory.setId(null);

        childrenSkillCategoryMapper.insert(childrenSkillCategory);
    }

    @Override
    public void deleteById(Integer id) {
        childrenSkillCategoryMapper.deleteById(id);
    }

    @Override
    public void updateById(ChildrenSkillCategoryDTO childrenSkillCategoryDTO) {
        if (childrenSkillCategoryDTO.getId() == null) {
            throw new GlobalException(ResponseCode.BAD_REQUEST, "ID不能为空！");
        }

        ChildrenSkillCategory childrenSkillCategory = toEntity(childrenSkillCategoryDTO);

        childrenSkillCategoryMapper.updateById(childrenSkillCategory);
    }

    @Override
    public void updateImageById(Integer id, String image) {
        imageUpdater.updateImageById(image, new Consumer<Object>() {
            @Override
            public void accept(Object o) {
                UpdateWrapper<ChildrenSkillCategory> updateWrapper = WrapperUtils.getUpdateWrapper(id, image);

                childrenSkillCategoryMapper.update(updateWrapper.getEntity(), updateWrapper);
            }
        });
    }

    @Override
    public Page<ChildrenSkillCategory> selectByPage(ChildrenSkillCategoryPager pager) {
        QueryWrapper<ChildrenSkillCategory> queryWrapper = new QueryWrapper<>();
        Page<ChildrenSkillCategory> page = Pager.ofPage(pager);

        queryWrapper.eq(
                StringUtils.isNotEmpty(pager.getName()),
                "name", pager.getName()
        );
        queryWrapper.eq(
                pager.getType() != null,
                "type", pager.getType()
        );
        queryWrapper.eq(
                pager.getSchoolId() != null,
                "school_id", pager.getSchoolId()
        );

        return childrenSkillCategoryMapper.selectPage(page, queryWrapper);
    }

    private ChildrenSkillCategory toEntity(ChildrenSkillCategoryDTO childrenSkillCategoryDTO) {
        ChildrenSkillCategory childrenSkillCategory = new ChildrenSkillCategory();

        childrenSkillCategory.setId(childrenSkillCategoryDTO.getId());
        childrenSkillCategory.setName(childrenSkillCategoryDTO.getName());
        childrenSkillCategory.setNote(childrenSkillCategoryDTO.getNote());
        childrenSkillCategory.setType(childrenSkillCategoryDTO.getType());
        childrenSkillCategory.setSchoolId(childrenSkillCategoryDTO.getSchoolId());
        childrenSkillCategory.setEfficacy(childrenSkillCategoryDTO.getEfficacy());
        childrenSkillCategory.setImproveEffect(childrenSkillCategoryDTO.getImproveEffect());

        return childrenSkillCategory;
    }

}